package cn.car91.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;
import cn.car91.bean.User;
import cn.car91.http.Car91HttpUtil;
import cn.car91.http.HttpHandler;
import cn.car91.service.UpdateService;
import cn.car91.utils.RegexUtil;

import com.baidu.android.pushservice.PushManager;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.lidroid.xutils.DbUtils;

public class InitApplication extends Application {
    private User user = null;
    private String domain = "car91.cn";
    private String androidServerUrl = "http://app.car91.cn/androidServer";
    private String centerServerUrl = "http://www.car91.cn/car91";
    private String articleServerUrl = "http://we.car91.cn/article";
    private String exposureServerUrl = "http://we.car91.cn/exposure";
    private String repairShopServerUrl = "http://we.car91.cn/repairshop";

    private String encryptCode = "hykj_car";

    LocationClient mLocationClient;
    LocationClientOption locationOption;
    public final int UPDATE_TIME_MUST_SHORT = 3 * 1000;    //3s刷新
    public final int UPDATE_TIME_SHORT = 10 * 1000;    //10s刷新
    public final int UPDATE_TIME_LONG = 5 * 60 * 1000;    //5min刷新
    public final int UPDATE_TIME_SUPER_LONG = 3 * 60 * 60 * 1000;    //3h刷新

    private Map<String, String> configsMap = new HashMap<String, String>();

    public final int apiIllegalId = 36;
    public final int apiWeatherId = 73;
    public final int apiOilId = 7;

    boolean isLocating = false;

    public boolean isLocating() {
        return isLocating;
    }

    public void setLocating(boolean isLocating) {
        this.isLocating = isLocating;
    }

    public LocationClient getmLocationClient() {
        return mLocationClient;
    }

    public void setmLocationClient(LocationClient mLocationClient) {
        this.mLocationClient = mLocationClient;
    }

    public LocationClientOption getLocationOption() {
        return locationOption;
    }

    public void setLocationOption(LocationClientOption locationOption) {
        this.locationOption = locationOption;
    }

    private DbUtils dbUtils;

    public DbUtils getDbUtils() {
        return dbUtils;
    }

    SharedPreferences backgroundPreferences = null;

    public SharedPreferences getBackgroundPreferences() {
        return backgroundPreferences;
    }

    SharedPreferences placePreferences = null;

    public SharedPreferences getPlacePreferences() {
        return placePreferences;
    }

    SharedPreferences timePreferences = null;

    public SharedPreferences getTimePreferences() {
        return timePreferences;
    }

    SharedPreferences otherPreferences;

    public SharedPreferences getOtherPreferences() {
        return otherPreferences;
    }

    SharedPreferences setPreferences = null;

    public SharedPreferences getSetPreferences() {
        return setPreferences;
    }

    SharedPreferences userPreferences = null;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        /* SharedPreferences */
        userPreferences = getSharedPreferences("user", Activity.MODE_PRIVATE);
        placePreferences = getSharedPreferences("place", Activity.MODE_PRIVATE);
        backgroundPreferences = getSharedPreferences("background", Activity.MODE_PRIVATE);
        timePreferences = getSharedPreferences("time", Activity.MODE_PRIVATE);
        otherPreferences = getSharedPreferences("other", Activity.MODE_PRIVATE);
        setPreferences = getSharedPreferences("set", Activity.MODE_PRIVATE);

        dbUtils = DbUtils.create(this,"carbd");
        dbUtils.configAllowTransaction(true);

        String uid = userPreferences.getString("uid", "");
        String uname = userPreferences.getString("uname", "");
        String upic = userPreferences.getString("upic", "");

        HttpHandler.httpHandler = new Car91HttpUtil(this);
        
        if (RegexUtil.isNotNull(uid) && RegexUtil.isNotNull(uname)) {
            user = new User();
            user.setId(uid);
            user.setName(uname);
            user.setPic(upic);
            setUser(user);
        }

        com.baidu.mapapi.SDKInitializer.initialize(this);
        //设置定位条件
        locationOption = new LocationClientOption();
        locationOption.setOpenGps(true);        //是否打开GPS
        locationOption.setCoorType("bd09ll");       //设置返回值的坐标类型。
        locationOption.setProdName("Car91"); //设置产品线名称。强烈建议您使用自定义的产品线名称，方便我们以后为您提供更高效准确的定位服务。
        locationOption.setScanSpan(UPDATE_TIME_SUPER_LONG);    //设置定时定位的时间间隔。单位毫秒

        mLocationClient = new LocationClient(this, locationOption);
        if (!mLocationClient.isStarted())
            mLocationClient.start();
        mLocationClient.registerLocationListener(new BDLocationListener() {
			@Override
			public void onReceiveLocation(BDLocation arg0) {
				if(arg0==null)
					mLocationClient.requestOfflineLocation();
				else{
					isLocating = true;
					if (mLocationClient.isStarted())
			            mLocationClient.stop();
				}
			}
		});
        if (mLocationClient.isStarted())
            mLocationClient.requestOfflineLocation();
        
        com.thinkland.sdk.android.SDKInitializer.initialize(this);
        
        /* 捕获异常 */
        /*GlobalException handler = GlobalException.getInstance();
        handler.init(getApplicationContext());*/

        com.baidu.frontia.FrontiaApplication.initFrontiaApplication(this);

        super.onCreate();
    }

    @Override
	public void onTerminate() {
		UpdateService.isFirst = true;
		super.onTerminate();
	}

	public String getAndroidServerUrl() {
        return androidServerUrl;
    }

    public String getCenterServerUrl() {
        return centerServerUrl;
    }

    public String getArticleServerUrl() {
        return articleServerUrl;
    }
    
    public String getRepairShopServerUrl() {
      return repairShopServerUrl;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
        SharedPreferences.Editor userEditor = userPreferences.edit();
        List<String> tags = new ArrayList<String>();
        if (user != null) {
            userEditor.putString("uid", user.getId());
            userEditor.putString("uname", user.getName());
            userEditor.putString("upic", user.getPic());
            tags.add("driver");
        } else {
            userEditor.clear();
            List<String> deltags = new ArrayList<String>();
            deltags.add("driver");
            PushManager.delTags(getApplicationContext(), deltags);
            tags.add("visitor");
        }
        PushManager.setTags(getApplicationContext(), tags);
        userEditor.commit();
    }

    public String getEncryptCode() {
        return encryptCode;
    }

    public String getExposureServerUrl() {
        return exposureServerUrl;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public Map<String, String> getConfigsMap() {
        return configsMap;
    }

    public void setConfigsMap(Map<String, String> configsMap) {
        this.configsMap = configsMap;
    }
}
